﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace UL360SurveyCommon
{
    public static class LoggerCommon
    {
        /// <summary>
        /// Log new entry in database, and optionaly in windows eventlog 
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="message"></param>
        /// <param name="loggingLevel"></param>
        /// <param name="logToEventlog"></param>
        /// <returns>ID that corresponds to entry in db with exception</returns>
        public static string LogNewEntry(LogNewEntryDelegate logger, string methodName, string message, int loggingLevel, bool logToEventlog = false, string eventLogName = "")
        {
            try
            {
                //Returnstring
                string returnStr = null;

                //Make new entry in db using the delegate
                returnStr = logger(methodName, message, loggingLevel);

                //If logging to eventlog
                if (logToEventlog)
                {
                    //Add ID from db to eventlog
                    message += "\n\nLogged ID to database: " + returnStr;
                    //Log to eventlog
                    LogNewEventlogEntry(methodName, message, loggingLevel, eventLogName);
                }

                return returnStr;
            }
            catch (Exception e)
            {
                //Write error to debugline
                Debug.WriteLine(e.ToString());
                return null;
            }
        }

        public delegate string LogNewEntryDelegate(string methodName, string message, int loggingLevel);

        /// <summary>
        /// Log to windows eventlog
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="message"></param>
        /// <param name="loggingLevel"></param>
        /// <param name="eventLogName"></param>
        public static void LogNewEventlogEntry(string methodName, string message, int loggingLevel, string eventLogName)
        {
            //Create eventlogsource if there is none
            if (!EventLog.SourceExists("UL360"))
            {
                EventLog.CreateEventSource("UL360", eventLogName);
            }

            //Eventlog entry type
            EventLogEntryType logType;

            //Set entry type
            switch((SurveyLoggingLevel)loggingLevel)
            {
                case SurveyLoggingLevel.Error:
                    logType = EventLogEntryType.Error;
                    break;
                case SurveyLoggingLevel.Warning:
                    logType = EventLogEntryType.Warning;
                    break;
                default:
                    logType = EventLogEntryType.Information;
                    break;
            }

            //Write to eventlog
            EventLog.WriteEntry("UL360", message + "\n\nMethodName where event occured: " + methodName, logType);
        }
    }
}
